package com.example.zx.theline.login;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.zx.theline.R;
import com.example.zx.theline.body.BodyActivity;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import GsonBean.IsLogin;
import app.MyApplication;
import base.BaseActivity;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import tools.MyToast;

public class LoginActivity extends BaseActivity {

    EditText mUsername, mPassword;
    String username,password;

    Button mButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        mUsername = (EditText) findViewById(R.id.username);
        mPassword = (EditText) findViewById(R.id.password);
        mButton = (Button) findViewById(R.id.login);
    }

    @Override
    protected void InitData() {

    }

    @Override
    protected void InitListen() {
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            username=mUsername.getText().toString();
            password=mPassword.getText().toString();
            if (username.equals("")||password.equals("")){
                MyToast.showToast("请填写用户名跟密码！");
                return;
            }
            RequestParams params=new RequestParams();
            params.add("userName",username);
            params.add("userPsw",password);
            NetWorkApi.login(params, new ObjectHttpResponseHandler(LoginActivity.this, IsLogin.class) {
                @Override
                public void Success(int i, Header[] headers, Object object) {
                  IsLogin mIsLogin= (IsLogin) object;
                    if(mIsLogin.getMsg().equals("failed")){
                        MyToast.showToast("用户名密码错误");
                    }
                    if(mIsLogin.getMsg().equals("success")){


                        Intent intent=new Intent(LoginActivity.this, BodyActivity.class);
                        startActivity(intent);
                    }
                }
            });

        }
    });
    }
}
